﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace EditUsers_Best.ManageUsersModule
{
    public class ManageUsersPresenter
    {
        IManageUsersView view;
        IUserRepository repository;

        public ManageUsersPresenter(IManageUsersView view, IUserRepository repository)
        {
            this.view = view;
            this.repository = repository;
            view.Presenter = this;
        }

        public void Search(string criteria)
        {
            if (!String.IsNullOrEmpty(criteria))
            {
                view.SetResults(repository.FindByName(criteria));
            }
            else
            {
                view.SetResults(repository.GetAll());
            }
        }

        public void Create(User user)
        {
            try
            {
                repository.Create(user);
                view.SetStatus(String.Format(
                    CultureInfo.CurrentCulture,
                    Properties.Resources.SavedUser,
                    user.FirstName, user.LastName));
            }
            catch (Exception)
            {
                view.SetStatus(String.Format(
                    CultureInfo.CurrentCulture,
                    Properties.Resources.SaveFailed,
                    user.FirstName, user.LastName));
            }
        }

        public void Save(User user)
        {
            if (repository.Update(user))
            {
                view.SetStatus(String.Format(
                    CultureInfo.CurrentCulture,
                    Properties.Resources.SavedUser,
                    user.FirstName, user.LastName));
            }
            else
            {
                view.SetStatus(String.Format(
                    CultureInfo.CurrentCulture,
                    Properties.Resources.SaveFailed,
                    user.FirstName, user.LastName));
            }
        }
    }
}
